package org.lanqiao.taru.lanqiao_demo.controller;

import org.lanqiao.taru.lanqiao_demo.common.Result;
import org.lanqiao.taru.lanqiao_demo.domain.Userinfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

@Controller
@RequestMapping("/userinfo")
public class UserinfoController {

    /**
     * 处理登录
     */
    @GetMapping("/login")
    @ResponseBody
    public Object login(@RequestParam(value = "username",required = true) String username,
                        @RequestParam(value = "password",required = false) String password){
        HashMap map = new HashMap();
        if("admin".equals(username) && "123".equals(password)){
            map.put("message","登录成功");
        }else {
                map.put("message","登录失败");
        }
        return map;
    }


    /**
     * 注册
     *
     * fetch("http://localhost:8080/reqister"),{
     *     method:"POST"
     *     body:JSON.stringify({
     *       username:"张三",
     *       possword:123456
     *     })
     * }
     */
    @PostMapping("/register")
    @ResponseBody
    public Object register(@RequestBody Userinfo userinfo){
        System.out.println("用户名:"+userinfo.getUsername());
        HashMap map = new HashMap();
        map.put("message",userinfo);
        return map;
    }



    //查询某个商品（根据主键查询）
    @GetMapping("/goods/{id}")
    @ResponseBody
    public Object queryGoods(@PathVariable("id") String id){
        //根据id查询商品信息
        System.out.println("前端传过来的编号是:"+id);
        // TODD 查询数据库

        ArrayList list = new ArrayList();
        list.add("商品1");
        list.add("商品2");

        return list;


    }
    //遍历D盘所有的文件名字

    @GetMapping("/list")
    @ResponseBody
    public Object list(){
        File file = new File("D:\\");
        String [] fileNames = file.list();
        return  fileNames;
    }




}
